import http from '@/utils/http'
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

export type Info = {
  [key: string]: unknown
}

export type NewsState = {
  info: Info
}

type GetRemind = {
  userid: string
}

type PutRemind = {
  userid: string
  applicant?: boolean
  approver?: boolean
}

export const getRemindAction = createAsyncThunk(
  'news/getRemindAction',
  async (payload: GetRemind) => {
    const res = await http.get('/news/remind', payload)
    return res.data
  }
)

export const putRemindAction = createAsyncThunk(
  'news/putRemindAction',
  async (payload: PutRemind) => {
    const res = await http.put('/news/remind', payload)
    return res.data
  }
)

const newsSlice = createSlice({
  name: 'news',
  initialState: {
    info: {},
  } as NewsState,
  reducers: {
    setInfo(state, action: PayloadAction<Info>) {
      state.info = action.payload
    },
  },
})

export const { setInfo } = newsSlice.actions

export default newsSlice.reducer
